import { useSelector, useDispatch } from 'react-redux'
import { updateAllTodo } from '../store/actions/todo'
import TodoItem from './TodoItem'

export default function TodoMain() {
  const dispatch = useDispatch()
  const { todo } = useSelector((state) => state)
  const handleChangeAll = (done) => dispatch(updateAllTodo(done))
  // !先确定全选的状态
  const isCheckedAll = todo.every((item) => item.done)
  return (
    <section className='main'>
      <input
        id='toggle-all'
        className='toggle-all'
        type='checkbox'
        checked={isCheckedAll}
        onChange={() => handleChangeAll(!isCheckedAll)}
      />
      <label htmlFor='toggle-all'>Mark all as complete</label>
      <ul className='todo-list'>
        {todo.map((item) => (
          <TodoItem key={item.id} item={item} />
        ))}
      </ul>
    </section>
  )
}
